1 /*
2 * Copyright 2003-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.velocity.tools.generic.directive;
18
19 import java.io.IOException;
20 import java.io.Writer;
21
22 import org.apache.velocity.context.InternalContextAdapter;
23 import org.apache.velocity.exception.MethodInvocationException;
24 import org.apache.velocity.exception.ParseErrorException;
25 import org.apache.velocity.exception.ResourceNotFoundException;
26 import org.apache.velocity.runtime.directive.Directive;
27 import org.apache.velocity.runtime.parser.node.Node;
28
29 /***
30 * @author <a href="mailto:shinobu@ieee.org">Shinobu Kawai</a>
31 * @version $Id: $
32 */
33 public class Ifnull extends Directive
34 {
35
36 /* (non-Javadoc)
37 * @see org.apache.velocity.runtime.directive.Directive#getName()
38 */
39 public String getName()
40 {
41 return "ifnull";
42 }
43
44 /* (non-Javadoc)
45 * @see org.apache.velocity.runtime.directive.Directive#getType()
46 */
47 public int getType()
48 {
49 return BLOCK;
50 }
51
52 /* (non-Javadoc)
53 * @see org.apache.velocity.runtime.directive.Directive#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer, org.apache.velocity.runtime.parser.node.Node)
54 */
55 public boolean render(InternalContextAdapter context, Writer writer,
56 Node node) throws IOException, ResourceNotFoundException,
57 ParseErrorException, MethodInvocationException
58 {
59 Object value = node.jjtGetChild(0).value(context);
60 if (value == null)
61 {
62 Node content = node.jjtGetChild(1);
63 content.render(context, writer);
64 }
65 return true;
66 }
67
68 }